<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <select name="branchid" lay-search="" id="branchTid">
                    <option value="">门店</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="search">搜索</button>
            </div>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test"></table>
<%--头部工具栏--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <%-- <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>--%>
        <button class="layui-btn layui-btn-sm" lay-event="panDian">新增盘点</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">刷新</button>
    </div>
</script>
<script>
    layui.use(['table','form','jquery','layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;

        //搜索填充
        window.onload=function (){ //页码加载完毕后执行的函数$(function(){})
            getAllBranch();
        }
        function getAllBranch(){
            $.ajax({
                url:"${pageContext.request.contextPath}/branch/findAll",
                typeP:"post",
                async:false,//ajax异步交互，async：false是同步提交
                success:function (result) {
                    var type=result.data;
                    var str="<option value=''>选择门店</option>";
                    for (var i = 0; i < type.length; i++) {
                        str+="<option value='"+type[i].branchid+"'>"+type[i].branchname+"</option>"
                    }
                    $("#branchTid").html(str);
                    $("#branch_id").html(str);
                    form.render('select'); //让form表单的select标签渲染
                },
                dataType:'json'
            });
        }
        //添加表单的搜索监听事件
        form.on('submit(search)',function(data){
            //表格数据重新渲染。
            table.reload('test',{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: { //按照什么进行查找
                    "branchid": data.field.branchid
                }
            })
        });

        //加载表格的实例
        table.render({
            id:"test",
            //elem element 元素---》表格的容器
            elem: '#demo'//table标签的id属性值
            ,url: '${pageContext.request.contextPath}/goods/getAllOutGoodsP' //数据接口
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field:'goodsid', title:'商品编号',fixed: 'left',sort: true}
                ,{field:'url', title:'商品图片',height:50,templet:function (data) {
                        var a=data.url
                        return "<Img src='"+a+"' style='width: 100px;height: 50px'>"
                    }}
                ,{field:'goodsname', title:'商品名称'}
                ,{field:'layout', title:'键盘布局'}
                ,{field:'goods.keycapid', title:'键帽材质',templet: function (data) {
                        //console.log(data);
                        return data.keycap==null?"":data.keycap.keycapname;
                    }}
                ,{field:'goods.keyboardcolorid', title:'键盘颜色',templet: function (data) {
                        //console.log(data);
                        //return data.keyboardcolor.keyboardcolorname;
                        return data.keyboardcolor==null?"":data.keyboardcolor.keyboardcolorname;
                    }}
                ,{field:'shafttype', title:'轴体类型'}
                ,{field:'goods.shaftchooseid', title:'轴体选择',templet: function (data) {
                        //console.log(data);
                        //return data.shaftchoose.shaftchoose;
                        return data.shaftchoose==null?"":data.shaftchoose.shaftchoose;
                    }}
                ,{field:'interfacetype', title:'接口类型'}
                ,{field:'goods.backlightcolorid', title:'背光颜色',templet: function (data) {
                        //console.log(data);
                        //return data.backlightcolor.backlightcolorname;
                        return data.backlightcolor==null?"":data.backlightcolor.backlightcolorname;
                    }}
                ,{field:'cprice', title:'商品进价'}
                ,{field:'sprice', title:'商品售价'}
                ,{field:'branchname', title:'店铺名称'}
                ,{field:'counts', title:'商品数量'}
                ,{field:'panDianNum', title: '盘点数量',edit:'text'}
                ,{field:'baoSunNum', title: '报损数量'}
            ]],
            text:{
                none:"无数据"
            },
            page:true,//开启分页
            limit:5,//默认每页显示的条数
            limits:[5,10,15,20]//自定义分页条数的选择
        });

        //头工具栏事件
        //toolbar()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                /**
                 * case后边的具体字符串跟 toolbarDemo中的 lay-event属性值一样
                 */
                case 'isAll':
                    table.reload("test");
                    break;
                case 'panDian':
                    if (checkStatus.data.length>0){
                        var data = checkStatus.data;
                        var flag=1;
                        var aaa=${sessionScope.staff.branchid};
                        $.each(data,function (i,val) {
                            var panDianNum =val.panDianNum;
                            var branchid= val.branchid;
                            $.ajax({
                                url: "${pageContext.request.contextPath}/inventory/selectPanDian",
                                type:"post",
                                dataType: "json",
                                // contentType:'application/json;charset=utf-8',
                                data:{"branchid":data.branchid},
                                success: function (result) {
                                    if (result.code==2) {
                                        //重载表格
                                        flag = 0;
                                        layer.msg("还有盘点未审核，无法新增盘点！");
                                    } else {
                                        flag = 1;
                                    }
                                }
                            });
                            if(branchid!=aaa){
                                flag=0;
                                layer.msg("对不起,你没有权限盘点该分店商品!")
                            }
                            if (panDianNum==null ||panDianNum=='' || isNaN(panDianNum)||!(/(^[1-9]\d*$)/.test(panDianNum))){
                                flag=0;
                                layer.msg("请检查你的数据格式")
                            }
                        })
                        if (flag==1){
                            layer.confirm('进行盘点申请吗？', function(index){
                                var data = checkStatus.data;
                                $.ajax({
                                    url: "${pageContext.request.contextPath}/inventory/addpanDian",
                                    type:"post",
                                    dataType: "json",
                                    // contentType:'application/json;charset=utf-8',
                                    data:{"panDianGoods":JSON.stringify(data)},
                                    success: function (result) {
                                        layer.closeAll();
                                        if (result.code==1) {
                                            //重载表格
                                            table.reload("test");
                                            layer.msg(result.msg);
                                        } else {
                                            layer.closeAll();
                                            layer.msg(result.msg);
                                        }
                                    }
                                });
                            });
                        }
                    } else {
                        alert("请选择要提交盘点的商品！")
                    }
                    break;
            };
        });

        //监听行工具事件------
        ////tool()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('edit(test)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //得到修改后的值
            console.log(obj);
            var name = obj.field; //当前编辑的字段名
            var data = obj.data;

            if (name=='panDianNum') {
                var panDianNum=obj.value;
                if (panDianNum==''||isNaN(panDianNum)){
                    $(this).val(0)
                    layer.msg("请检查数据格式！")
                }else {
                    var aaa=data.counts-data.panDianNum;
                    obj.update({
                        baoSunNum:aaa
                    })
                }
                if (panDianNum>obj.data.counts){
                    $(this).val(obj.data.counts);
                    layer.msg("输入数量有误");
                    obj.update({
                        baoSunNum:0
                    });
                    table.reload('test')
                }else {
                    var aaa=data.counts-data.panDianNum;
                    obj.update({
                        baoSunNum:aaa
                    });
                }
                if (panDianNum < 0) {
                    $(this).val(0)
                    layer.msg("盘点数据有误！")
                    obj.update({
                        baoSunNum:data.counts
                    })
                }

            }
        });
    });
</script>
</body>
</html>
